﻿using System.Collections.Generic;
using System.Linq;

namespace rpxnow_library.membership.extensions
{
    internal static class Users
    {
        /// <summary>
        /// Gets the primary openid identifier for a given user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        static public rpxnow_library.membership.OpenIdMembershipUser.UserIdentifier GetPrimaryOpenIdIdentifier(this rpxnow_library.membership.data.User user)
        {
            if (user == null)
                throw new System.ArgumentNullException("user");
                        
            var userIdentifier = user.UserIdentifiers.FirstOrDefault();
            if (userIdentifier != null)
            {
                return userIdentifier.CreateOpenIdMembershipUserIdentifier();
            }

            return null;
        }

        /// <summary>
        /// Gets the openid identifiers for a given user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        static public IEnumerable<rpxnow_library.membership.OpenIdMembershipUser.UserIdentifier> GetOpenIdentifiers(this rpxnow_library.membership.data.User user)
        {
            if (user == null)
                throw new System.ArgumentNullException("user");

            var userIdentifiers = new List<rpxnow_library.membership.OpenIdMembershipUser.UserIdentifier>();

            foreach (var ui in user.UserIdentifiers)
            {
                userIdentifiers.Add(ui.CreateOpenIdMembershipUserIdentifier());
            }
            return userIdentifiers;
        }

        static public rpxnow_library.membership.OpenIdMembershipUser.UserIdentifier CreateOpenIdMembershipUserIdentifier(this rpxnow_library.membership.data.UserIdentifier userIdentifier)
        {
            if (userIdentifier == null)
                throw new System.ArgumentNullException("userIdentifier");

            return new rpxnow_library.membership.OpenIdMembershipUser.UserIdentifier() { Identifier = userIdentifier.identifier, ProviderName = userIdentifier.providerName };
        }
    }
}
